package com.example.renderscripttest

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v8.renderscript.Allocation
import android.support.v8.renderscript.RenderScript
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Thread {
            val rs = RenderScript.create(this)
            val sHello = ScriptC_hello(rs)// 根据rs/hello.rs自动生成类

            val inBitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
            val outBitmap = Bitmap.createBitmap(inBitmap.width, inBitmap.height, inBitmap.config)
            val inA = Allocation.createFromBitmap(rs, inBitmap)
            val outA = Allocation.createFromBitmap(rs, outBitmap)

            sHello.forEach_invert(inA, outA)
            outA.copyTo(outBitmap)

            runOnUiThread {
                imageView2.setImageBitmap(inBitmap)
                imageView.setImageBitmap(outBitmap)
            }
            rs.destroy()

        }.start()
    }
}
